<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\OperationLogs;
use App\Model\TrendsComment;
use App\Model\TrendsContent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class TrendsController extends BaseController
{
    public function __construct(Request $request, TrendsContent $trendsContent)
    {
        parent::__construct($request);
        $this->model = $trendsContent;
        $this->commontModel = new TrendsComment();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $list = $this->model->getList($request);
        $list = objToArray($list);
        $status = $request->input('status');
        foreach ($list['data'] as &$v) {
            $log = $this->model->getAuditLog($v['content_id']);
            foreach ($log as $item){
                if (!isset($v['first_time'])){
                    $v['first_time'] = $item['check_time'];
                    $v['first_name'] = $item['name'];
                    continue;
                }
                $v['second_time'] = $item['check_time'];
                $v['second_name'] = $item['name'];
            }
            $v['audit_count'] = count($log);
            $v['audit_status'] = $log[$v['audit_count'] - 1]['status'] ?? 3;
            if ($status == -1) {
                $v['opt_name'] = $log[$v['audit_count'] - 1]['name'] ?? '暂无';
            } elseif ($status == 0) {
                $v['opt_name'] = '暂无';
            } else {
                $v['opt_name'] = $log[$status]['name'] ?? '暂无';
            }
        }
        $waitAuditTotal = $this->model->countWaitAudit();
        return $this->toJson(self::SUCCESS, ['list' => $list, 'wait_audit_total' => $waitAuditTotal]);
    }

    public function getTrends(Request $request)
    {
        $id = $request->input('id') ?? 0;
        $res = $this->model->getTrendsFirst($id);
        return $this->toJson(self::SUCCESS, $res);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        $data['content_id'] = $id;
        $isForbid = $request->input('is_forbid', 1);
        $res = $this->model->updateData($data);
        if ($isForbid === 0) {
            $user_id = $request->input('user_id');
            $res = curl_request(getKkaudioApiUrl() . '/inner/user/forbid_user', ['user_id' => $user_id, 'timeout' => 0], 'json', 'POST');
        }
        if ($res['code'] !== 0) {
            return $this->toJson(self::ERROR, $res);
        }
        OperationLogs::createData($request, OperationLogs::TRENDS, "删除", $id);
        return $this->toJson(self::SUCCESS);
    }

    //查看评论
    public function trendsComment(Request $request)
    {
        $res = $this->commontModel->getList($request->input());
        foreach ($res['data'] as &$v) {
            $v->create_date = date('Y-m-d H:i:s', $v->create_timestamp);
        }
        return $this->toJson(self::SUCCESS, $res);
    }


    //动态管理一键审核（通过初审）
    public function oneClickAudit(Request $request)
    {
        $admin_user = $this->user;
        $list = $this->model->getList($request);
        $list = objToArray($list);

        $data = $list['data'];
        foreach ($data as $k => $v) {
            if($v['status'] == 0) {
                $this->auditTrend($request, $v['content_id']);
            }
        }

        return $this->toJson(self::SUCCESS);
//        $ids = implode(',',array_column($data,'id'));
//        $res = $contentCheck->setContentStatus($ids , $status, $audit_type,  $admin_user['id']);
//        if($res['code'] == 0){
//
//        }
//        return $this->toJson(self::ERROR,$res['msg']);
    }

    public function deleteComment(Request $request)
    {
        $comment_id = $request->input('comment_id');
        return $this->toJson(self::SUCCESS, $this->commontModel->deleteComment($comment_id));
    }

    public function update(Request $request, $id)
    {
        $type = $request->input('type', 0);
        $res = $this->model->updateTrend($id, $type);
        if ($res['code'] === 0) {
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR, [], $res['msg']);
    }

    public function auditTrend(Request $request, $id)
    {
        $state = (int)$request->input('state');
        $audit_type = (int)$request->input('audit_type', 1);
        $admin_user = $this->user;
        //插入记录
        $res = $this->model->auditTrend($id, $state);
        if ($res['code'] === 0) {
            $this->model->createLog($id, $state, $audit_type, $admin_user['id']);
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR, [], $res['msg']);
    }

    public function switchState(Request $request)
    {
        $status = (int)$request->input('state');
        $res = $this->model->switchState($status);
        Log::debug($res);
        if ($res['code'] === 0) {
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR, [], $res['err']);
    }

    public function getSwitchState()
    {
        return $this->toJson(self::SUCCESS, $this->model->getSwitchState());
    }

    public function getTrendsRecommend(){
        return $this->toJson(self::SUCCESS ,$this->model->getRecommendList());
    }

    public function createOrUpdate(Request $request){
        $params = $request->all();
        $res = $this->model->createOrUpdateRecommend($params);
        if ($res['code'] === 0) {
            return $this->toJson(self::SUCCESS);
        }
        return $this->toJson(self::ERROR, [], $res['msg']);
    }

    public function deleteRecommend(Request $request, $id)
    {
        $res = $this->model->deleteRecommend(['id' => $id]);
        if ($res['code'] !== 0) {
            return $this->toJson(self::ERROR, [], $res['msg']);
        }
        return $this->toJson(self::SUCCESS);
    }
}
